/*
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

@title: BananaBeatdown
@author: FammyBoy
@description: None
@tags: []
@addedOn: 2024-00-00
*/

const player = "p"
const floor = "f"
const sky = "s"
const eleft = "l"
const eright = "r"
const chargemeter = "m"
const cardsprite = "c"
const explosion = "e"
const beam = "b"
const fire = "i"

let skyloop = 0
let cardspin = true
let leftspawn = true
let playeridle = false
let eidle = false
let anim = false
let left = false
let combo = 1
let score = 0
let charge = 0
let emovespeed = 1000
let espawnrate = 1000
let fireloop = 0

let startup = true

let card = "N/A"

let game = false

setLegend(
  [player, bitmap`
................
..........00....
........0000....
......00660.....
.0..506660......
0.0.05660000....
0.0.05505...0...
0..0C5605...0...
00.0C660...0.0..
0..0C56605..0...
...0C6555.......
...0CC6660......
....0CCCC00.....
...0000000000...
..0......00..0..
.00...........00`],
  [floor, bitmap`
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
11LL111111111111
11.LLL1111111.L1
1....LL.L1111..L
1.....L.L111...L
1.......L11.....
L........1......
................
................
................
................`],
  [sky, bitmap`
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222`],
  [eleft, bitmap`
.....DD.........
....D..D........
........D.......
......HHD.......
...HH.HHHD.HH...
...HHH0HHDHH0...
....HH.0HHD0H...
....HHH00H00....
...000H0HHH000..
..0.HHHDHHDHH.0.
..0.HHHH00DHH.0.
.000..H0DH0..000
..0...HHDHH...0.
......0.HH.0....
.....0..HH..0...
....00......00..`],
  [eright, bitmap`
.........DD.....
........D..D....
.......D........
.......DHH......
...HH.DHHH.HH...
...0HHDHH0HHH...
...H0DHH0.HH....
....00H00HHH....
..000HHH0H000...
.0.HHDHHDHHH.0..
.0.HHD00HHHH.0..
000..0HD0H..000.
.0...HHDHH...0..
....0.HH.0......
...0..HH..0.....
..00......00....`],
  [chargemeter, bitmap`
.....111111.....
..111000000111..
.11000000000011.
.L0000000000001.
.L0000000000001.
L000000000000001
L000000000000001
L000000000000001
L000000000000001
L000000000000001
L000000000000001
.L0000000000001.
.L0000000000001.
.LL0000000000L1.
..LLL000000LLL..
.....LLLLLL.....`],
  [cardsprite, bitmap`
....CCCCCCCC....
...CL0L0L0L0C...
...C0L0L0L0LC...
...CL0L0L0L0C...
...C0L0L0L0LC...
...CL0L0L0L0C...
...C0L0L0L0LC...
...CL0L0L0L0C...
...C0L0L0L0LC...
...CL0L0L0L0C...
...C0L0L0L0LC...
...CL0L0L0L0C...
...C0L0L0L0LC...
...CL0L0L0L0C...
...C0L0L0L0LC...
....CCCCCCCC....`]
)

let level = 0

setSolids([player, explosion])

const levels = [
  map`
c.......m
.........
.........
....p....
fffffffff`,
  map`
.....
.....
.....
.....`
]

setBackground(sky)

function moveenemy() {
  let alleleft = getAll(eleft);

  for (let i = 0; i < alleleft.length; i++) {
    if (alleleft[i]) {
      alleleft[i].x += 1;
    }
  }

  let alleright = getAll(eright);

  for (let n = 0; n < alleright.length; n++) {
    if (alleright[n]) {
      alleright[n].x -= 1;
    }
  }
}

function updatecharge() {
  if (charge === 0) {
    setLegend(
      [chargemeter, bitmap`
.....111111.....
..111000000111..
.11000000000011.
.L0000000000001.
.L0000000000001.
L000000000000001
L000000000000001
L000000000000001
L000000000000001
L000000000000001
L000000000000001
.L0000000000001.
.L0000000000001.
.LL0000000000L1.
..LLL000000LLL..
.....LLLLLL.....`]
    )
  } else if (charge === 1) {
    setLegend(
      [chargemeter, bitmap`
.....111111.....
..111000000111..
.11000000000011.
.L0000000000001.
.L0000000000001.
L000000000000001
L000000000000001
L000000000000001
L000000000000001
L000000000000001
L000000000000001
.L0000000000001.
.L0000000000001.
.LL5577700007L1.
..LLL557775LLL..
.....LLLLLL.....`]
    )
  } else if (charge === 2) {
    setLegend(
      [chargemeter, bitmap`
.....111111.....
..111000000111..
.11000000000011.
.L0000000000001.
.L0000000000001.
L000000000000001
L000000000000001
L000000000000001
L000000000000001
L000000000000001
L000000000000001
.L7750005750071.
.L5777757777751.
.LL5777777575L1.
..LLL557755LLL..
.....LLLLLL.....`]
    )
  } else if (charge === 3) {
    setLegend(
      [chargemeter, bitmap`
.....111111.....
..111000000111..
.11000000000011.
.L0000000000001.
.L0000000000001.
L000000000000001
L000000000000001
L000000000000001
L000000000000001
L550000000055001
L555555055555551
.L5577557772751.
.L5722722277751.
.LL5557277555L1.
..LLL557755LLL..
.....LLLLLL.....`]
    )
  } else if (charge === 4) {
    setLegend(
      [chargemeter, bitmap`
.....111111.....
..111000000111..
.11000000000011.
.L0000000000001.
.L0000000000001.
L000000000000001
L000000000000001
L550000005555551
L555555555777751
L577777777727751
L557722222227551
.L5777722227751.
.L5557772277551.
.LL5557777755L1.
..LLL555555LLL..
.....LLLLLL.....`]
    )
  } else if (charge === 5) {
    setLegend(
      [chargemeter, bitmap`
.....111111.....
..111000000111..
.11000000000011.
.L0000000000001.
.L5000000000001.
L555500000005551
L577555577755751
L577775777777751
L572277772227751
L577227222227751
L557222222277551
.L5777222227751.
.L5777722277551.
.LL5577777755L1.
..LLL555555LLL..
.....LLLLLL.....`]
    )
  } else if (charge === 6) {
    setLegend(
      [chargemeter, bitmap`
.....111111.....
..111000000111..
.11000000000011.
.L5550055555001.
.L5755557777551.
L557757772277551
L572277222227751
L572222222222751
L572222222222751
L572222222227751
L572222222227551
.L5722222227751.
.L5777222277551.
.LL5577777555L1.
..LLL555555LLL..
.....LLLLLL.....`]
    )
  } else if (charge === 7) {
    setLegend(
      [chargemeter, bitmap`
.....111111.....
..111777777111..
.11772222227711.
.L7222222222271.
.L7222222222271.
L722222222222271
L722222222222271
L722222222222271
L722222222222271
L722222222222271
L722222222222271
.L7222222222271.
.L7222222222271.
.LL7722222277L1.
..LLL777777LLL..
.....LLLLLL.....`]
    )
  } else {
    setLegend(
      [chargemeter, bitmap`
..555111111555..
.51117777771115.
5117722222277115
5L72222222222715
5L72222222222715
L722222222222271
L722222222222271
L722222222222271
L722222222222271
L722222222222271
L722222222222271
5L72222222222715
5L72222222222715
5LL7722222277L15
.5LLL777777LLL5.
..555LLLLLL555..`]
    )
  }
}

function pattack() {
  if(game === true) {
  if (anim === false) {
    anim = true
    if (left === true) {

      if (combo === 1) {
        combo = 2
        setLegend(
          [player, bitmap`
......000.......
......00000.....
.......06660.0..
........06665.0.
.........0650..0
........50550.0.
...0....506500..
...000000000C0..
.......50665C0..
........5556C0..
.......0666CC0..
......00CCCC0...
......00000000..
....0000......00
..00.........00.
00..........0000`]
        )
        setTimeout(() => {
          setLegend(
            [player, bitmap`
0000............
006600000...0...
.066666600.0.0..
..0006666500..0.
.7..00665600....
222.7505560.27..
227..50056C0...2
0000000666C0....
27..5..056C0....
222775556CC0....
..72700000C0....
...70000CC00.7..
......00000....2
...7.0..00.0....
............0...
...........00...`]
          )
          clearTile(getFirst(player).x - 1, 3)
          getFirst(player).x -= 1
          clearTile(getFirst(player).x - 1, 3)
        }, 100)

        setTimeout(() => {
          anim = false
          setLegend(
            [player, bitmap`
................
................
...00...........
...0000.........
....06600.......
.....066605.....
.....006650..0..
...00050550.0.0.
..0...5065C0..0.
.0.0...066C0.00.
..0..50665C0..0.
......5556C0....
.....0666CC0....
..0000CCCC00....
..0.000000..0...
00..00.......00.`]
          )
        }, 250);

      } else if (combo === 2) {
        clearTile(getFirst(player).x - 1, 3)
        getFirst(player).x -= 1
        clearTile(getFirst(player).x - 1, 3)
        combo = 3
        setLegend(
          [player, bitmap`
................
................
................
................
...........00...
..........0000..
.........50005..
..........5050..
.........55055..
..........0650..
..........0600..
.......2.050C0..
.......77060C0..
.....777.00C000.
....72200.0000.0
.....0077.00..00`]
        )

        setTimeout(() => {
          setLegend(
            [player, bitmap`
................
................
................
................
............00..
............000.
.........5..050.
..........50560.
........05505500
......70.0065C00
..7.7.0.5.060C0.
.77777020550C00.
7.7777706660C0..
772222706C0C00..
72222270C00070..
7222270000.7.00.`]
          )
        }, 100)

        setTimeout(() => {
          setLegend(
            [player, bitmap`
................
................
................
................
..........00....
..........000...
.........0000...
...7.7...0565...
.....7..05565...
7....7.0776650..
.7..7.0.5.6760..
.7777702077777..
7.7777772222277.
7777777222222277
7277727772222227
7777272222222227`]
          )
        }, 200)

        setTimeout(() => {
          anim = false
          setLegend(
            [player, bitmap`
................
................
...00...........
...0000.........
....06600.......
.....066605.....
.....006650..0..
...00050550.0.0.
..0...5065C0..0.
.0.0...066C0.00.
..0..50665C0..0.
......5556C0....
.....0666CC0....
..0000CCCC00....
..0.000000..0...
00..00.......00.`]
          )
        }, 300);

      } else if (combo === 3) {
        combo = 4
        setLegend(
          [player, bitmap`
................
................
................
................
................
................
.......000.....0
......006605..0.
.....0000650.0..
.....000055C0...
........06500...
.0000000666C0...
....0006665C0...
....000555CC0.0.
...0000CCCC00000
.00...00000....0`]
        )
        setTimeout(() => {
          clearTile(getFirst(player).x - 1, 3)
          getFirst(player).x -= 1
          clearTile(getFirst(player).x - 1, 3)
          setLegend(
            [player, bitmap`
.....7777277....
..770202222777..
7702770666002772
0027706666060.77
0222706666660007
02277055CCC00007
2277066C50077007
777.0650.5..77.7
....0650....7777
....05C05...777.
....000......727
.....00....7.77.
...........7..77
............2.7.
.........7...7..
........2...77.7`]
          )
        }, 100)
        setTimeout(() => {
          setLegend(
            [player, bitmap`
...77....7...7..
....77..........
.727..7..7.72.0.
2727....77...0..
727..00.0...0...
270000000C000...
770706660.0000..
770.06655006C0..
72070656056CC0..
7777060650CC00..
.722000CCCC00...
.7222000C000..7.
.77222020077727.
..7722027722777.
...77222227777..
.....7077777....`]
          )
        }, 200)
        setTimeout(() => {
          anim = false
          setLegend(
            [player, bitmap`
................
................
...00...........
...0000.........
....06600.......
.....066605.....
.....006650..0..
...00050550.0.0.
..0...5065C0..0.
.0.0...066C0.00.
..0..50665C0..0.
......5556C0....
.....0666CC0....
..0000CCCC00....
..0.000000..0...
00..00.......00.`]
          )
        }, 300);
      } else {
        combo = 1
        setLegend(
          [player, bitmap`
......000.7.0...
......00000.0..0
.......06660..00
.......706665.0.
....7.7..06500..
........50550..7
........506500..
..7..7..0666C0.7
.......50665C0..
........5556C0..
...77..0666CC0..
...7..00CCCC0...
......000000007.
.7..0000......70
..00.......7700.
00.....777..0000`]
        )
        setTimeout(() => {
          setLegend(
            [player, bitmap`
......000.7.0...
......00000.0..0
.......06660..00
.......706665.0.
....7.7..06500..
........50550..7
........506500..
..7..7..0666C0.7
.......50665C0..
........5556C0..
...77..0666CC0..
...7..00CCCC0...
......000000007.
.7..0000......70
..00.......7700.
00.....777..0000`]
          )
        }, 50)

        setTimeout(() => {
          setLegend(
            [player, bitmap`
.........727.7.0
........77227707
.......7.5..0507
.....7777.505607
......2.2.55560.
.....7.72.505607
...7.777..066C77
....7...50665C72
...72....5556C77
...727..0666CC0.
....7..00CCCC07.
.......000000027
..7..0000......7
...00.......7700
.00.....777..000
................`]
          )
        }, 100)

        setTimeout(() => {
          setLegend(
            [player, bitmap`
.........7777770
.........7772277
.........5.72227
..........572227
..........557770
..........505600
.........0066C00
........50665C00
....2....5556C77
.........006CC00
........0000000.
.......0......07
..7...0........0
.....0......77.0
....0...777....0
...00..........0`]
          )
        }, 200)

        setTimeout(() => {
          clearTile(getFirst(player).x - 1, 3)
          getFirst(player).x -= 1
          clearTile(getFirst(player).x - 1, 3)
          setLegend(
            [player, bitmap`
....777777777...
000722222777777.
066000002227777.
0666666007722777
.00066665077..77
7220066560..77.7
2227505560....27
227.50056C0...77
222000660C0.77..
220000006C077727
22775556CC7772.7
7727.70777072.77
7722272222222.7.
.7277222227777..
.7727777777.0...
...00.7777.000..`]
          )
        }, 250)
        setTimeout(() => {
          setLegend(
            [player, bitmap`
....7...7777....
000722........7.
066000002.....7.
066666600....777
.00066665......7
7..0066560.....7
...7505560....27
2...50056C0...77
000000660C0..7..
000000006C0..727
2.775556CC77.2.7
7..7.70777072.77
7..2202222022.7.
..2.022....777..
.7.2.07.....0...
...0007....000..`]
          )
        }, 400)
        setTimeout(() => {
          anim = false
          setLegend(
            [player, bitmap`
................
................
...00...........
...0000.........
....06600.......
.....066605.....
.....006650..0..
...00050550.0.0.
..0...5065C0..0.
.0.0...066C0.00.
..0..50665C0..0.
......5556C0....
.....0666CC0....
..0000CCCC00....
..0.000000..0...
00..00.......00.`]
          )
        }, 500);
      }






    } else if (left === false) {

      if (combo === 1) {
        combo = 2
        setLegend(
          [player, bitmap`
.......000......
.....00000......
..0.06660.......
.0.56660........
0..0560.........
.0.05505........
..005605....0...
..0C000000000...
..0C56605.......
..0C6555........
..0CC6660.......
...0CCCC00......
..00000000......
00......0000....
.00.........00..
0000..........00`]
        )
        setTimeout(() => {
          clearTile(getFirst(player).x + 1, 3)
          getFirst(player).x += 1
          clearTile(getFirst(player).x + 1, 3)
          setLegend(
            [player, bitmap`
............0000
...0...000006600
..0.0.006666660.
.0..0056666000..
....00656600..7.
..72.0655057.222
2...0C65005..722
....0C6660000000
....0C650..5..72
....0CC655577222
....0C00000727..
..7.00CC00007...
2....00000......
....0.00..0.7...
...0............
...00...........`]
          )
        }, 100)

        setTimeout(() => {
          anim = false
          setLegend(
            [player, bitmap`
................
................
...........00...
.........0000...
.......00660....
.....506660.....
..0..056600.....
.0.0.05505000...
.0..0C5605...0..
.00.0C660...0.0.
.0..0C56605..0..
....0C6555......
....0CC6660.....
....00CCCC0000..
...0..000000.0..
.00.......00..00`]
          )
        }, 250);

      } else if (combo === 2) {
        clearTile(getFirst(player).x + 1, 3)
        getFirst(player).x += 1
        clearTile(getFirst(player).x + 1, 3)
        combo = 3
        setLegend(
          [player, bitmap`
................
................
................
................
...00...........
..0000..........
..50005.........
..0505..........
..55055.........
..0560..........
..0060..........
..0C050.2.......
..0C06077.......
.000C00.777.....
0.0000.00227....
00..00.7700.....`]
        )

        setTimeout(() => {
          setLegend(
            [player, bitmap`
................
................
................
................
..00............
.000............
.050..5.........
.06505..........
00550550........
00C5600.07......
.0C060.5.0.7.7..
.00C05502077777.
..0C0666077777.7
..00C0C607222277
..07000C07222227
.00.7.0000722227`]
          )
        }, 100)

        setTimeout(() => {
          setLegend(
            [player, bitmap`
................
................
................
................
....00..........
...000..........
...0000.........
...5650...7.7...
...56550..7.....
..0566770.7....7
..0676.5.0.7..7.
..7777702077777.
.7722222777777.7
7722222227777777
7222222777277727
7222222222727777`]
          )
        }, 200)

        setTimeout(() => {
          anim = false
          setLegend(
            [player, bitmap`
................
................
...........00...
.........0000...
.......00660....
.....506660.....
..0..056600.....
.0.0.05505000...
.0..0C5605...0..
.00.0C660...0.0.
.0..0C56605..0..
....0C6555......
....0CC6660.....
....00CCCC0000..
...0..000000.0..
.00.......00..00`]
          )
        }, 300);

      } else if (combo === 3) {
        combo = 4
        setLegend(
          [player, bitmap`
................
................
................
................
................
................
0.....000.......
.0..506600......
..0.0560000.....
...0C550000.....
...00560........
...0C6660000000.
...0C5666000....
.0.0CC555000....
00000CCCC0000...
0....00000...00.`]
        )
        setTimeout(() => {
          clearTile(getFirst(player).x + 1, 3)
          getFirst(player).x += 1
          clearTile(getFirst(player).x + 1, 3)
          setLegend(
            [player, bitmap`
....7727777.....
..777222202077..
2772006660772077
77.0606666077200
7000666666072220
70000CCC55077220
70077005C6607722
7.77..5.0560.777
7777....0560....
.777...50C50....
727......000....
.77.7....00.....
77..7...........
.7.2............
..7...7.........
7.77...2........`]
          )
        }, 100)
        setTimeout(() => {
          setLegend(
            [player, bitmap`
..7...7....77...
..........77....
.0.27.7..7..727.
..0...77....7272
...0...0.00..727
...000C000000072
..0000.066607077
..0C60055660.077
..0CC65065607027
..00CC0560607777
...00CCCC000227.
.7..000C0002227.
.72777002022277.
.7772277202277..
..77772222277...
....7777707.....`]
          )
        }, 200)
        setTimeout(() => {
          anim = false
          setLegend(
            [player, bitmap`
................
................
...........00...
.........0000...
.......00660....
.....506660.....
..0..056600.....
.0.0.05505000...
.0..0C5605...0..
.00.0C660...0.0.
.0..0C56605..0..
....0C6555......
....0CC6660.....
....00CCCC0000..
...0..000000.0..
.00.......00..00`]
          )
        }, 300);
      } else {
        combo = 1
        setLegend(
          [player, bitmap`
...0.7.000......
0..0.00000......
00..06660.......
.0.566607.......
..00560..7.7....
7..05505........
..005605........
7.0C6660..7..7..
..0C56605.......
..0C6555........
..0CC6660..77...
...0CCCC00..7...
.700000000......
07......0000..7.
.0077.......00..
0000..777.....00`]
        )
        setTimeout(() => {
          setLegend(
            [player, bitmap`
...0.7.000......
0..0.00000......
00..06660.......
.0.566607.......
..00560..7.7....
7..05505........
..005605........
7.0C6660..7..7..
..0C56605.......
..0C6555........
..0CC6660..77...
...0CCCC00..7...
.700000000......
07......0000..7.
.0077.......00..
0000..777.....00`]
          )
        }, 50)

        setTimeout(() => {
          setLegend(
            [player, bitmap`
0.7.727.........
70772277........
7050..5.7.......
706505.7777.....
.06555.2.2......
706505.27.7.....
77C660..777.7...
27C56605...7....
77C6555....27...
.0CC6660..727...
.70CCCC00..7....
720000000.......
7......0000..7..
0077.......00...
000..777.....00.
................`]
          )
        }, 100)

        setTimeout(() => {
          setLegend(
            [player, bitmap`
0777777.........
7722777.........
72227.5.........
722275..........
077755..........
006505..........
00C6600.........
00C56605........
77C6555....2....
00CC600.........
.0000000........
70......0.......
0........0...7..
0.77......0.....
0....777...0....
0..........00...`]
          )
        }, 200)

        setTimeout(() => {
          clearTile(getFirst(player).x + 1, 3)
          getFirst(player).x += 1
          clearTile(getFirst(player).x + 1, 3)
          setLegend(
            [player, bitmap`
...777777777....
.777777222227000
.777722200000660
7772277006666660
77..77056666000.
7.77..0656600227
72....0655057222
77...0C65005.722
..77.0C066000222
727770C600000022
7.2777CC65557722
77.27077707.7277
.7.2222222722277
..7777222227727.
...0.7777777277.
..000.7777.00...`]
          )
        }, 250)
        setTimeout(() => {
          setLegend(
            [player, bitmap`
....7777...7....
.7........227000
.7.....200000660
777....006666660
7......56666000.
7.....0656600..7
72....0655057...
77...0C65005...2
..7..0C066000000
727..0C600000000
7.2.77CC655577.2
77.27077707.7..7
.7.2202222022..7
..777....220.2..
...0.....70.2.7.
..000....7000...`]
          )
        }, 400)
        setTimeout(() => {
          anim = false
          setLegend(
            [player, bitmap`
................
................
...........00...
.........0000...
.......00660....
.....506660.....
..0..056600.....
.0.0.05505000...
.0..0C5605...0..
.00.0C660...0.0.
.0..0C56605..0..
....0C6555......
....0CC6660.....
....00CCCC0000..
...0..000000.0..
.00.......00..00`]
          )
        }, 500);
      }
    }
  }
}
}

function spawnenemy() {
  if (game === true) {
      setLegend(
        [eleft, bitmap`
.....222........
....2DDD2.......
...2D222D2......
...222HHD2.22...
..2HH2HHHD2HH2..
..2HHH0HHDHH02..
.2.2HH20HHD0H2..
20200HH00H002.22
200220H0HHH02202
.222HHHDHHDHH022
...2HHHH00DHH202
...222H0DH0.2.2.
..2002HHDHH.2...
...20002HH2002..
....2222HH2202..
........22.2002.`],
        [eright, bitmap`
........222.....
.......2DDD2....
......2D222D2...
....222DHH22....
...2HHDHHHH2....
..2HHHDH0HHH2...
..20HDD0HHHH2...
..200H00DDH2....
..20HHH0DHH2....
.20H0HHHH0H2....
..20H00H00H22...
.20200D0022202..
..22H0000000202.
....22002222.2..
..220022........
.20022..........`]
      )
    if (leftspawn === true) {
      leftspawn = false
      addSprite(0, 3, eleft)
    } else {
      leftspawn = true
      addSprite(8, 3, eright)
    }
  }
}

function testfordeath() {
  if (tilesWith(player, eleft).length > 0 || tilesWith(player, eright).length > 0) {
    setLegend(
      [sky, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`]
    )
    setMap(levels[1])
    game = false
    addText("Youre ded btw", { x: 4, y: 8, colour: color`2` })
  }
}

function resetemove() {
  if (game === true) {
    emovespeed = 750 - (score * 2)

    clearInterval(emoveinterval)

    emoveinterval = setInterval(() => {
      moveenemy()
      testfordeath

      resetemove()
    }, emovespeed)
  } else {
    clearInterval(emoveinterval)
  }
}

function resetespawn() {
  if (game === true) {
    espawnrate = 750 - (score * 2)

    clearInterval(espawninterval)

    espawninterval = setInterval(() => {
      spawnenemy()
      testfordeath()
      resetespawn()
    }, espawnrate)
  } else {
    clearInterval(espawninterval)
  }
}

function resetcard() {
  card = "N"
  cardspinning = setInterval(() => {
    if (cardspin === true) {
      cardspin = false
      setLegend(
        [cardsprite, bitmap`
.......CC.......
.......CC.......
.......CC.......
.......CC.......
.......CC.......
.......CC.......
.......CC.......
.......CC.......
.......CC.......
.......CC.......
.......CC.......
.......CC.......
.......CC.......
.......CC.......
.......CC.......
.......CC.......`]
      )
    } else {
      cardspin = true
      setLegend(
        [cardsprite, bitmap`
....CCCCCCCC....
...CL0L0L0L0C...
...C0L0L0L0LC...
...CL0L0L0L0C...
...C0L0L0L0LC...
...CL0L0L0L0C...
...C0L0L0L0LC...
...CL0L0L0L0C...
...C0L0L0L0LC...
...CL0L0L0L0C...
...C0L0L0L0LC...
...CL0L0L0L0C...
...C0L0L0L0LC...
...CL0L0L0L0C...
...C0L0L0L0LC...
....CCCCCCCC....`]
      )
    }
  }, 100)
  setTimeout(() => {
    let randomnumber = Math.floor(Math.random() * 3) + 1
    clearTimeout(cardspinning)
    if (randomnumber === 1) {
      card = "green"
      setLegend(
        [cardsprite, bitmap`
.4.DCCCCCCCCD.4.
4.DC44444444CD.4
4.DC44422444CD.4
4.DC44222244CD.4
4.DC42222224CD.4
4.DC42222224CD.4
4.DC42422424CD.4
4.DC44422444CD.4
4.DC44422444CD.4
4.DC44422444CD.4
4.DC44422444CD.4
4.DC44422444CD.4
4.DC44422444CD.4
4.DC44444444CD.4
4.DC44444444CD.4
.4.DCCCCCCCCD.4.`]
      )
    } else if(randomnumber === 2) {
      card = "blue"
      setLegend(
        [cardsprite, bitmap`
.7.5CCCCCCCC5.7.
7.5C77777777C5.7
7.5C77777777C5.7
7.5C72727777C5.7
7.5C77722777C5.7
7.5C77772277C5.7
7.5C77777227C5.7
7.5C72222227C5.7
7.5C72222227C5.7
7.5C77777227C5.7
7.5C77772277C5.7
7.5C77772777C5.7
7.5C27227777C5.7
7.5C77777777C5.7
7.5C77777777C5.7
.7.5CCCCCCCC5.7.`]
      )
    } else {
            card = "red"
      setLegend(
        [cardsprite, bitmap`
.3.3CCCCCCCC3.3.
3.3C33323333C3.3
3.3C33323333C3.3
3.3C33322333C3.3
3.3C33322333C3.3
3.3C32232323C3.3
3.3C32332223C3.3
3.3C22233233C3.3
3.3C22323323C3.3
3.3C23332322C3.3
3.3C23323332C3.3
3.3C32322232C3.3
3.3C32332332C3.3
3.3C33233323C3.3
3.3C33322233C3.3
.3.3CCCCCCCC3.3.`]
      )
    }
  }, 1500)
}

function greenult() {
  left = false
  anim = true
  setLegend(
    [player, bitmap`
................
................
................
................
..0........0....
.00...4.4.00....
0..04.000.0.0...
...0.06660......
....0066000.....
....0560000.....
....0556000.....
....0C6660......
...40C56654.....
...40CC55600.4..
..4400CCCC0000..
.000..000000.40.`]
  )
  setTimeout(() => {
    setLegend(
      [player, bitmap`
......4.........
..4..4.4........
.0...4.4.4...44.
.0...444..40....
.0..44224..0...4
4.0...444.0.44..
..0.444.440.42..
.4.0.424404.42..
.4.04000004..4..
42440660002444..
.42006600044..4.
.4.00C00002244..
42400C60002224.4
.4200CC66600224.
4.2200CCCC0000..
400022000000220.`]
    )
    setTimeout(() => {


      getFirst(player).y -= 1
      setLegend(
        [explosion, bitmap`
................
................
................
................
.......4........
......4.4.......
......4LLL......
.....L4LLL......
.....LL4LLL.4...
....4LLLLLL..4..
....4LLLLLL.LL4.
...L4LLLLLLLLL4.
..LLL4LLLLLLLL4.
..LLLLLLLLLLLL4.
..LLLLLLL4LLL4..
...LLLLLLL444...`]
      )
      clearTile(getFirst(player).x - 2, 3)
      clearTile(getFirst(player).x - 1, 3)
      clearTile(getFirst(player).x, 3)
      clearTile(getFirst(player).x + 1, 3)
      clearTile(getFirst(player).x + 2, 3)


      addSprite(getFirst(player).x - 2, 3, explosion)
      addSprite(getFirst(player).x - 1, 3, explosion)
      addSprite(getFirst(player).x, 3, explosion)
      addSprite(getFirst(player).x + 1, 3, explosion)
      addSprite(getFirst(player).x + 2, 3, explosion)
      setLegend(
        [player, bitmap`
.....400........
.....400........
.....400........
.....000........
.....060........
.....060........
....0660........
....05605.......
...005665.......
...0056650......
...0056650......
...00C66000.....
...005C65.0.....
..000C5C5.0.....
..0..0505.0.....
..0...05..0.....`]
      )
      setTimeout(() => {
        setLegend(
          [player, bitmap`
...........0....
...........00...
.......00.0.....
....42000.0.....
...4200000...0.0
..0..066005.0.0.
..0006560.5.0...
.0.00C55055.0...
0...0056045.00..
00..0C56604.00..
....0CC6650060..
...40CC5556660..
....200C55C60...
....44.00000....
........004.4...
...00000........`]
        )
        setTimeout(() => {
          setLegend(
            [explosion, bitmap`
..........44....
.........4444...
..44444.44LL4L..
.4444LL44LLLLL..
.44LLLLL4L11LL..
444LL11L4L111LL.
44LLL11L4L1111L.
4LLL111L411111LL
4LLL1111L111LLLL
.4LL1111.11L444.
.LL111.1..11L44.
.LL11.....11LL44
.LL11......LLL44
.LL11.....1LLL44
..LL1..1.1L4L444
...LL1..11L.444.`],
            [player, bitmap`
...........0....
...44......00...
..4424.00.0.....
..422.000.0.....
.42..00000...0.0
..0..066005.0.0.
..00065604540...
.0.00C5505540...
0...00560.5400..
00..0C5660..00..
....0CC6650060..
.4..0CC5556660..
.42..00C55C60...
.4422..00000.24.
..444...00..244.
...00000...444..`]
          )
          setTimeout(() => {
            setLegend(
              [explosion, bitmap`
.4......4..4....
4......4........
....444.....L...
....4...........
.............L..
...L.L.......L..
................
.L..............
..............4.
................
................
...............4
..............L.
.............L..
............4..4
................`],
              [player, bitmap`
..44444....0....
.4422244...00...
.42....00.0.....
4...2.000.0.....
..2..000004..0.0
..0..06600540.0.
..0006560.520...
.0.00C55055.04..
0...00560.5.00..
00..0C5660..00..
....0CC6650060..
4...0CC5556660.4
2.2..00C55C60..4
42.22..00000.224
442.....00..2.24
.4400000.....444`]
            )

            setTimeout(() => {
              clearTile(getFirst(player).x - 2, 3)
              clearTile(getFirst(player).x - 1, 3)
              clearTile(getFirst(player).x, 3)
              clearTile(getFirst(player).x + 1, 3)
              clearTile(getFirst(player).x + 2, 3)
              setTimeout(() => {
                getFirst(player).y += 1
                anim = false
                setLegend(
                  [player, bitmap`
.....400........
.....400........
.....400........
.....000........
.....060........
.....060........
....0660........
....05605.......
...005665.......
...0056650......
...0056650......
...00C66000.....
...005C65.0.....
..000C5C5.0.....
..0..0505.0.....
..0...05..0.....`]
                )
                setTimeout(() => {
                  setLegend(
                    [player, bitmap`
................
................
...........00...
.........0000...
.......00660....
.....506660.....
..0..056600.....
.0.0.05505000...
.0..0C5605...0..
.00.0C660...0.0.
.0..0C56605..0..
....0C6555......
....0CC6660.....
....00CCCC0000..
...0..000000.0..
.00.......00..00`]
                  )
                }, 100)
              }, 100)
            }, 100)
          }, 100)
        }, 100)
      }, 100)
    }, 100)
  }, 100)
}

function blueult() {
  if(anim === false) {
    if(left === false) {
  anim = true
  setTimeout(() => {
    setLegend(
      [player, bitmap`
................
..77............
..722777........
.72027.77.......
..07............
..0.0000...7....
...006660..7....
...0066000.77...
...0560000......
.000556000......
0..0C6660.......
0..0C5665....7..
.020CC55600.....
.2277CC7C0700...
.7707707770..0..
00...........00.`]
    )
    setTimeout(() => {
      setLegend(
        [player, bitmap`
................
..77............
..722007........
.72000077.......
.00660..........
066660.....7....
05660......7....
065605.....77...
060500777.......
06500.727.......
0666C0777.......
7777C5.......7..
72275C00000.....
7227CCCCC0.00...
.77000000....0..
00...........00.`]
      )
      setTimeout(() => {
        setLegend(
          [player, bitmap`
................
................
...............7
.........0000077
.....00506607777
...0000000007777
..06656566607772
.06566CC55507722
.065CCC050..7222
.0C55C0500007222
.0CC555.....7722
00CC0.......7722
000000......7772
000..0......7777
......0......777
......00.......7`],
          [beam, bitmap`
................
................
7777777777777777
7777777777777777
7777777777777777
7777777777777777
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
7777777777777777
7777777777777777
7777777777777777`]
        )
        for (let i = getFirst(player).x + 1; i < 9; i++) {
          clearTile(i, 3)
          addSprite(i, 3, beam)
        }
        setTimeout(() => {
        setLegend(
          [player, bitmap`
.........00.....
.......0000.....
......0660......
.....0660.......
....0660........
....56605.......
..000660........
.0006500........
.0.065000.......
.0.0C50.00......
...0CC0.00......
...0CC0.........
...0000.........
...0000.........
..0.00.0........
00......00......`],
          [beam, bitmap`
..7.............
777......7......
7777....77777..7
7777.....777...7
77..............
.......777......
.7.........2277.
777...222..77772
22..2222......22
..2.....2222....
...777....22....
...7777.........
................
77.........77777
....7777.777...7
....777........7`]
        )
        for (let i = getFirst(player).x + 1; i < 9; i++) {
          clearTile(i, 3)
          addSprite(i, 3, beam)
        }
setTimeout(() => {
  anim = false
        setLegend(
          [player, bitmap`
................
................
...........00...
.........0000...
.......00660....
.....506660.....
..0..056600.....
.0.0.05505000...
.0..0C5605...0..
.00.0C660...0.0.
.0..0C56605..0..
....0C6555......
....0CC6660.....
....00CCCC0000..
...0..000000.0..
.00.......00..00`]
        )
        for (let i = getFirst(player).x + 1; i < 9; i++) {
          clearTile(i, 3)
        }
  }, 100)
        }, 100)
      }, 100)
    }, 100)
  }, 100)
    } else {
  anim = true
  setTimeout(() => {
    setLegend(
      [player, bitmap`
................
............77..
........777227..
.......77.72027.
............70..
....7...0000.0..
....7..066600...
...77.0006600...
......0000650...
......000655000.
.......0666C0..0
..7....5665C0..0
.....00655CC020.
...0070C7CC7722.
..0..0777077077.
.00...........00`]
    )
    setTimeout(() => {
      setLegend(
        [player, bitmap`
................
............77..
........700227..
.......77000027.
..........06600.
....7.....066660
....7......06650
...77.....506560
.......777005060
.......727.00560
.......7770C6660
..7.......5C7777
.....00000C57227
...00.0CCCCC7227
..0....00000077.
.00...........00`]
      )
      setTimeout(() => {
        setLegend(
          [player, bitmap`
................
................
7...............
7700000.........
77770660500.....
7777000000000...
27770666565660..
22770555CC66560.
2227..050CCC560.
2227000050C55C0.
2277.....555CC0.
2277.......0CC00
2777......000000
7777......0..000
777......0......
7.......00......`],
          [beam, bitmap`
................
................
7777777777777777
7777777777777777
7777777777777777
7777777777777777
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
7777777777777777
7777777777777777
7777777777777777`]
        )
        for (let i = 0; i < getFirst(player).x; i++) {
          clearTile(i, 3)
          addSprite(i, 3, beam)
        }
        setTimeout(() => {
        setLegend(
          [player, bitmap`
.....00.........
.....0000.......
......0660......
.......0660.....
........0660....
.......50665....
........066000..
........0056000.
.......000560.0.
......00.05C0.0.
......00.0CC0...
.........0CC0...
.........0000...
.........0000...
........0.00.0..
......00......00`],
          [beam, bitmap`
..7.............
777......7......
7777....77777..7
7777.....777...7
77..............
.......777......
.7.........2277.
777...222..77772
22..2222......22
..2.....2222....
...777....22....
...7777.........
................
77.........77777
....7777.777...7
....777........7`]
        )
        for (let i = 0; i < getFirst(player).x; i++) {
          clearTile(i, 3)
          addSprite(i, 3, beam)
        }
setTimeout(() => {
  anim = false
        setLegend(
          [player, bitmap`
................
................
...00...........
...0000.........
....06600.......
.....066605.....
.....006650..0..
...00050550.0.0.
..0...5065C0..0.
.0.0...066C0.00.
..0..50665C0..0.
......5556C0....
.....0666CC0....
..0000CCCC00....
..0.000000..0...
00..00.......00.`]
        )
        for (let i = 0; i < getFirst(player).x; i++) {
          clearTile(i, 3)
        }
  }, 100)
        }, 100)
      }, 100)
    }, 100)
  }, 100)
    }
  }
}

function redult() {
  if(anim === false) {
    anim = true
    if(left === false) {
setTimeout(() => {
        setLegend(
          [player, bitmap`
........00.33.3.
.......0603323..
......06603233..
......06603223..
.....56600.33...
.....056050.....
.....05555......
....005605......
...00C660.......
..0.0C5605......
..0.0C5555......
.0..0C6660......
.00.0CC66600....
....00CCC00000..
...0..000000.0..
.00...........00`]
        )
  setTimeout(() => {
        setLegend(
          [player, bitmap`
....00...33.....
....00.33....33.
...0663333.33..3
...0663233333...
...0633223333...
..50332223323...
..056322222333..
..055322222333..
..05632223333...
..0C6333333.....
..0C666605......
.0.0C666600.....
.000CCC66600....
....00CCC00000..
...0..000000.0..
.00...........00`]
        )
      setTimeout(() => {
        setLegend(
          [player, bitmap`
................
.0........00....
0.0.....0000....
0..0...06600....
.0..0.56660.....
..0.0056660.....
....065336033...
....055330.33...
...506655553.3..
...5C365002.333.
...5C666030.333.
...05350.3.22.33
..00CC33323.2223
.0.0000330.32222
0..0000333032222
0...00...3.33222`],
          [fire, bitmap`
................
................
................
.........3......
.........3..3...
....3....3..3...
...3333333.3333.
...333333332233.
...333333332233.
...332233333333.
..3333222233333.
333333222222333.
3222223322223333
2232333322223323
2222222222222233
2222222222222233`]
        )
        let fireloc = getFirst(player).x + 1
        addSprite(fireloc, 3, fire)
        setTimeout(() => {
          anim = false
                  setLegend(
          [player, bitmap`
................
................
...........00...
.........0000...
.......00660....
.....506660.....
..0..056600.....
.0.0.05505000...
.0..0C5605...0..
.00.0C660...0.0.
.0..0C56605..0..
....0C6555......
....0CC6660.....
....00CCCC0000..
...0..000000.0..
.00.......00..00`]
        )
          
        fireanimr = setInterval(() => {
          if(fireloop === 0) {
            fireloop = 1
        setLegend(
          [fire, bitmap`
.....3..........
....3...........
....3.........3.
....33........33
....33........33
..3.333..3....33
3.33323333...33.
3.33323333..333.
3.3322332333323.
333222232232223.
.33222222222233.
.33222223222223.
.332222322222223
.332223322222223
.333322222222333
..3332222222333.`]
        )
            clearTile(fireloc, 3)
          addSprite(fireloc, 3, fire)
          } else if (fireloop === 1) {
            fireloop = 2
                    setLegend(
          [fire, bitmap`
.........33.....
.......3..33....
....333....33...
.3..333....33...
.3..3333...3....
.3..3333.3.333..
.3..3333333333..
.3333223322233..
..332222222333..
..322322222223..
332222232222333.
322222332222233.
322222222222233.
332222222222333.
33322222222333..
.3333333333333..`]
        )
            clearTile(fireloc, 3)
          addSprite(fireloc, 3, fire)
          } else if(fireloop === 2){
            fireloop = 0
                    setLegend(
          [fire, bitmap`
.......33..3.3..
.....3333..33...
33...3333..33...
................
...333........3.
...33333.3.3333.
3...3333333323..
...33233332233..
..3322323323323.
..33233222232233
..33222223322233
..33222222222223
..33222222222223
..33322222222223
...3333222222333
.....333333333..`]
        )
            clearTile(fireloc, 3)
          addSprite(fireloc, 3, fire)
          }
        }, 100)
          }, 100)
        setTimeout(() => {
clearInterval(fireanimr)
          clearTile(fireloc, 3)
        }, 5000)
  }, 100)
  }, 100)
  }, 100)
    } else {
      setTimeout(() => {
        setLegend(
          [player, bitmap`
.3.33.00........
..3233060.......
..33230660......
..32230660......
...33.00665.....
.....050650.....
......55550.....
......506500....
.......066C00...
......5065C0.0..
......5555C0.0..
......0666C0..0.
....00666CC0.00.
..00000CCC00....
..0.000000..0...
00...........00.`]
        )
  setTimeout(() => {
        setLegend(
          [player, bitmap`
.....33...00....
.33....33.00....
3..33.3333660...
...3333323660...
...3333223360...
...32332223305..
..333222223650..
..333222223550..
...33332223650..
.....3333336C0..
......506666C0..
.....006666C0.0.
....00666CCC000.
..00000CCC00....
..0.000000..0...
00...........00.`]
        )
      setTimeout(() => {
        setLegend(
          [player, bitmap`
................
....00........0.
....0000.....0.0
....00660...0..0
.....06665.0..0.
.....0666500.0..
...330633560....
...33.033550....
..3.355556605...
.333.200563C5...
.333.030666C5...
33.22.3.05350...
3222.32333CC00..
22223.0330000.0.
2222303330000..0
22233.3...00...0`],
          [fire, bitmap`
................
................
................
......3.........
...3..3.........
...3..3....3....
.3333.3333333...
.332233333333...
.332233333333...
.333333332233...
.3333322223333..
.333222222333333
3333222233222223
3233222233332322
3322222222222222
3322222222222222`]
        )
        let fireloc = getFirst(player).x - 1
        addSprite(fireloc, 3, fire)
        setTimeout(() => {
          anim = false
                  setLegend(
          [player, bitmap`
................
................
...00...........
...0000.........
....06600.......
.....066605.....
.....006650..0..
...00050550.0.0.
..0...5065C0..0.
.0.0...066C0.00.
..0..50665C0..0.
......5556C0....
.....0666CC0....
..0000CCCC00....
..0.000000..0...
00..00.......00.`]
        )
          
        fireaniml = setInterval(() => {
          if(fireloop === 0) {
            fireloop = 1
        setLegend(
          [fire, bitmap`
.....3..........
....3...........
....3.........3.
....33........33
....33........33
..3.333..3....33
3.33323333...33.
3.33323333..333.
3.3322332333323.
333222232232223.
.33222222222233.
.33222223222223.
.332222322222223
.332223322222223
.333322222222333
..3332222222333.`]
        )
            clearTile(fireloc, 3)
          addSprite(fireloc, 3, fire)
          } else if (fireloop === 1) {
            fireloop = 2
                    setLegend(
          [fire, bitmap`
.........33.....
.......3..33....
....333....33...
.3..333....33...
.3..3333...3....
.3..3333.3.333..
.3..3333333333..
.3333223322233..
..332222222333..
..322322222223..
332222232222333.
322222332222233.
322222222222233.
332222222222333.
33322222222333..
.3333333333333..`]
        )
            clearTile(fireloc, 3)
          addSprite(fireloc, 3, fire)
          } else if(fireloop === 2){
            fireloop = 0
                    setLegend(
          [fire, bitmap`
.......33..3.3..
.....3333..33...
33...3333..33...
................
...333........3.
...33333.3.3333.
3...3333333323..
...33233332233..
..3322323323323.
..33233222232233
..33222223322233
..33222222222223
..33222222222223
..33322222222223
...3333222222333
.....333333333..`]
        )
            clearTile(fireloc, 3)
          addSprite(fireloc, 3, fire)
          }
        }, 100)
          }, 100)
        setTimeout(() => {
clearInterval(fireaniml)
          clearTile(fireloc, 3)
        }, 5000)
  }, 100)
  }, 100)
  }, 100)
  }
}
}

function startgameloop() {

game = true
  
setLegend(
  [player, bitmap`
................
..........00....
........0000....
......00660.....
.0..506660......
0.0.05660000....
0.0.05505...0...
0..0C5605...0...
00.0C660...0.0..
0..0C56605..0...
...0C6555.......
...0CC6660......
....0CCCC00.....
...0000000000...
..0......00..0..
.00...........00`],
  [floor, bitmap`
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
11LL111111111111
11.LLL1111111.L1
1....LL.L1111..L
1.....L.L111...L
1.......L11.....
L........1......
................
................
................
................`],
  [sky, bitmap`
6699999999999966
6699999999999966
6699999999999966
6699999999999966
6699999999999966
6699999999999966
6699999999999966
6699999999999966
6699999999999966
6699999999999966
6699999999999966
6699999999999966
6699999999999966
6699999999999966
6699999999999966
6699999999999966`],
  [eleft, bitmap`
.....DD.........
....D..D........
........D.......
......HHD.......
...HH.HHHD.HH...
...HHH0HHDHH0...
....HH.0HHD0H...
....HHH00H00....
...000H0HHH000..
..0.HHHDHHDHH.0.
..0.HHHH00DHH.0.
.000..H0DH0..000
..0...HHDHH...0.
......0.HH.0....
.....0..HH..0...
....00......00..`],
  [eright, bitmap`
.........DD.....
........D..D....
.......D........
.......DHH......
...HH.DHHH.HH...
...0HHDHH0HHH...
...H0DHH0.HH....
....00H00HHH....
..000HHH0H000...
.0.HHDHHDHHH.0..
.0.HHD00HHHH.0..
000..0HD0H..000.
.0...HHDHH...0..
....0.HH.0......
...0..HH..0.....
..00......00....`],
  [chargemeter, bitmap`
.....111111.....
..111000000111..
.11000000000011.
.L0000000000001.
.L0000000000001.
L000000000000001
L000000000000001
L000000000000001
L000000000000001
L000000000000001
L000000000000001
.L0000000000001.
.L0000000000001.
.LL0000000000L1.
..LLL000000LLL..
.....LLLLLL.....`],
  [cardsprite, bitmap`
....CCCCCCCC....
...CL0L0L0L0C...
...C0L0L0L0LC...
...CL0L0L0L0C...
...C0L0L0L0LC...
...CL0L0L0L0C...
...C0L0L0L0LC...
...CL0L0L0L0C...
...C0L0L0L0LC...
...CL0L0L0L0C...
...C0L0L0L0LC...
...CL0L0L0L0C...
...C0L0L0L0LC...
...CL0L0L0L0C...
...C0L0L0L0LC...
....CCCCCCCC....`]
)
  
setMap(levels[level])


resetcard()


emoveinterval = setInterval(() => {
  moveenemy()
  testfordeath

  resetemove()
}, emovespeed)

espawninterval = setInterval(() => {
  spawnenemy()
  testfordeath()
  resetespawn()
}, espawnrate)






onInput("j", () => {
  left = true
  pattack()
})

onInput("l", () => {
  left = false
  pattack()
})

onInput("k", () => {
  if(game === true) {
  if (anim === false) {
    if (charge <= 7) {
      anim = true
      setLegend(
        [player, bitmap`
................
................
................
...7...7.77.....
..7.7..777..7...
....775000007...
....700566000...
...7.00550500...
..7700C5605000..
.07700C66077.0..
.0.700C566050.7.
.0.070C6555077..
..0700CC66600...
...77000CC0007..
....0000000007..
..00...77..000..`]
      )
      setTimeout(() => {
        setLegend(
          [player, bitmap`
....7..7...77...
...7..7.........
..77.77..7..7.7.
...7.7.7.77..77.
..7.7..72.7..77.
7..7.772277277..
...7..00002277.7
.7.2200660027.7.
.77200566000.07.
0.7.00550000707.
0.220056650007..
0.7200C66660227.
.0.0005C6560027.
..07.0055C0000..
....00000000007.
..000.......000.`]
        )
      }, 200)
      setTimeout(() => {
        setLegend(
          [player, bitmap`
....7.....00....
...77....000....
.......00000....
.....500660.....
......5660....7.
.....05560......
.....05605......
.000006600000...
00...0660....0..
.....056605....7
..7..06555....7.
.....006660.....
.....000CC00....
....0...0000....
..00......00..7.
................`]
        )
      }, 400)
      setTimeout(() => {
        anim = false
        charge = charge + 1
        updatecharge()
        setLegend(
          [player, bitmap`
................
................
...........00...
.........0000...
.......00660....
.....506660.....
..0..056600.....
.0.0.05505000...
.0..0C5605...0..
.00.0C660...0.0.
.0..0C56605..0..
....0C6555......
....0CC6660.....
....00CCCC0000..
...0..000000.0..
.00.......00..00`]
        )
      }, 500)
    }
  }
  }
})

onInput("i", () => {
  if (charge >= 2 && anim === false) {
    if (card === "green") {
      charge -= 2
      updatecharge()
      greenult()
      resetcard()
    } else if (card === "blue") {
      charge -= 2
      updatecharge()
      blueult()
      resetcard()
    } else if(card === "red") {
      charge -= 2
      updatecharge()
      redult()
      resetcard()
    }
  }
})

onInput("a", () => {
  if(game === true) {
  if (anim === false) {
    combo = 1
    anim = true
    left = true
    setLegend(
      [player, bitmap`
................
................
................
...........0....
.000000...00....
00666600.0.0....
000666650.0.....
..00665600......
...50556000.....
...50056CC0.....
.0..05005CC0....
.0.0..55CCC0....
..0..0000C00....
....0...0000....
.....00..00.00..
......0.......0.`]
    )
    getFirst(player).x -= 1
    testfordeath()
    setTimeout(() => {
      anim = false
      setLegend(
        [player, bitmap`
................
................
...00...........
...0000.........
....06600.......
.....066605.....
.....006650..0..
...00050550.0.0.
..0...5065C0..0.
.0.0...066C0.00.
..0..50665C0..0.
......5556C0....
.....0666CC0....
..0000CCCC00....
..0.000000..0...
00..00.......00.`]
      )
    }, 150)
  }
  }
})

onInput("d", () => {
  if(game === true) {
  if (anim === false) {
    combo = 1
    left = false
    anim = true
    setLegend(
      [player, bitmap`
................
................
................
....0...........
....00...000000.
....0.0.00666600
.....0.056666000
......00656600..
.....00065505...
.....0CC65005...
....0CC50050..0.
....0CCC55..0.0.
....00C0000..0..
....0000...0....
..00.00..00.....
.0.......0......`]
    )
    getFirst(player).x += 1
    testfordeath()
    setTimeout(() => {
      anim = false
      setLegend(
        [player, bitmap`
................
................
...........00...
.........0000...
.......00660....
.....506660.....
..0..056600.....
.0.0.05505000...
.0..0C5605...0..
.00.0C660...0.0.
.0..0C56605..0..
....0C6555......
....0CC6660.....
....00CCCC0000..
...0..000000.0..
.00.......00..00`]
      )
    }, 150)
  }
  }
})





eaniminterval = setInterval(() => {
  if (game === true) {
    if (eidle === false) {
      eidle = true
      setLegend(
        [eleft, bitmap`
.....222........
....2DDD2.......
...2D222D2......
...222HHD2.22...
..2HH2HHHD2HH2..
..2HHH0HHDHH02..
.2.2HH20HHD0H2..
20200HH00H002.22
200220H0HHH02202
.222HHHDHHDHH022
...2HHHH00DHH202
...222H0DH0.2.2.
..2002HHDHH.2...
...20002HH2002..
....2222HH2202..
........22.2002.`],
        [eright, bitmap`
........222.....
.......2DDD2....
......2D222D2...
....222DHH22....
...2HHDHHHH2....
..2HHHDH0HHH2...
..20HDD0HHHH2...
..200H00DDH2....
..20HHH0DHH2....
.20H0HHHH0H2....
..20H00H00H22...
.20200D0022202..
..22H0000000202.
....22002222.2..
..220022........
.20022..........`]
      )
    } else {
      eidle = false
      setLegend(
        [eleft,bitmap`
.....222........
....2DDD2.......
...2D222D2......
....22HHD222....
....2HHHHDHH2...
...2HHH0HDHHH2..
...2HHHH0DDH02..
....2HDD00H002..
....2HHD0HHH02..
....2H0HHHH0H02.
...22H00H00H02..
..2022200D00202.
.2020000000H22..
..2.22220022....
........220022..
..........22002.`],
        [eright, bitmap`
........222.....
.......2DDD2....
......2D222D2...
...22.2DHH222...
..2HH2DHHH2HH2..
..20HHDHH0HHH2..
..2H0DHH02HH2.2.
22.200H00HH00202
20220HHH0H022002
220HHDHHDHHH222.
202HHD00HHHH2...
.2.2.0HD0H222...
...2.HHDHH2002..
..2002HH20002...
..2022HH2222....
.2002.22........`]
      )
    }
  }
}, 125)

skyinterval = setInterval(() => {
  if(skyloop === 0) {
    skyloop = 1
  setLegend(
[sky, bitmap`
6666999999999666
6666999999999666
6666999999999666
6666999999999666
6669999999999666
6999999999999966
6699999999999966
6699999999999996
6669999999999996
6669999999999996
6669999999999966
6666999999999966
6669999999999966
6699999999999966
6999999999999996
6669999999999966`]
    )
  } else if(skyloop === 1) {
    skyloop = 0
      setLegend(
[sky, bitmap`
6669999999999996
6699999999999996
6999999999999996
6999999999999966
6699999999999966
6669999999999666
6666999999999666
6666999999999666
6666699999996666
6666699999966666
6666699999966666
6666999999996666
6699999999999966
6999999999999996
6699999999999996
6666999999999996`]
    )
  }
}, 300)


scoreinterval = setInterval(() => {
  if (game === true) {
    score = score + 1


    addText("score:", { x: 8, y: 0, colour: color`2` })
    addText(`${score}`, { x: 10, y: 1, color: color`2` })
  }
}, 1000)
}

setMap(levels[1])


addText("j/l to attack", { x: 4, y: 2, colour: color`2` })
addText("a/d to dash", { x: 4, y: 4, colour: color`2` })
addText("k to charge", { x: 5, y: 6, colour: color`2` })
addText("i to ultimate", { x: 4, y: 8, colour: color`2` })
addText("w to start/restart ", { x: 1, y: 12, colour: color`2` })

onInput("w", () => {
  if(game === false) {
    if(startup === false) {
        clearInterval(emoveinterval)
    clearInterval(espawninterval)
    clearInterval(eaniminterval)
    clearInterval(skyinterval)
    clearInterval(scoreinterval)
      score = 0
    }
  clearText()
  startgameloop()
    startup = false
    game = true
  }
})